package com.google.fbyoutube.client.widget;

import com.google.fbyoutube.client.YouTube;
import com.google.fbyoutube.shared.FBUtility;
import com.google.fbyoutube.shared.ytVideoEntry;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class WidThumb extends Composite {

  private static int MAX_TITLE_LEN = 40;

  /**
   * @wbp.parser.constructor
   * @param video TODO
   */
  public WidThumb(final ytVideoEntry video) {

		if (video != null) {
			this.item = video;
	
			VerticalPanel verticalPanel = new VerticalPanel();
			verticalPanel.setStyleName("thumbBox");
			verticalPanel.setSpacing(2);
			initWidget(verticalPanel);
			verticalPanel.setSize("130px", "140px");
			
			ClickHandler cl = new ClickHandler() {
	          public void onClick(ClickEvent event) {
	            YouTube.getAppGUI().watchVideo(video);
	            }
	        };
	        
	        String imgUrl;
	        if (video.getMediaGroupThmbnails().size() > 0) {
	        	imgUrl = video.getMediaGroupThmbnails().get(0);
	        } else {
	        	imgUrl = FBUtility.THUMBNAIL_DEFAULT;
	        }
			Image image = new Image(imgUrl);
			image.setStyleName((String) null);
			verticalPanel.add(image);
			verticalPanel.setCellHeight(image, "60%");
			image.setSize("128px", "72px");
			image.addClickHandler(cl);
	
			String text = video.getShortTitle();
			InlineLabel VodTitle = new InlineLabel(text);
			VodTitle.addClickHandler(cl);
			VodTitle.setDirectionEstimator(true);
			VodTitle.setStyleName("thumbBox-title");
			verticalPanel.add(VodTitle);
			VodTitle.setSize("124px", "27px");
			verticalPanel.setCellHeight(VodTitle, "20%");
	
			float rating = video.getRateAverage();
			InlineLabel nlnlblRate = new InlineLabel("Rating " + rating);
	//		verticalPanel.add(nlnlblRate);
			nlnlblRate.setWidth("100%");
			
			String views = String.valueOf(video.getViews()) + "views";
			InlineLabel nlnlblViews = new InlineLabel(views);
			nlnlblViews.setStyleName("thumbBox-views");
	//		verticalPanel.add(nlnlblViews);
			
			String author = video.getShortAuthor();
			InlineLabel nlnlblauthor = new InlineLabel(author);
			nlnlblauthor.setStyleName("thumbBox-views");
			verticalPanel.add(nlnlblauthor);
		} else {
			// mock preview
			SimplePanel sp = new SimplePanel();
//			verticalPanel.setStyleName("thumbBox");
//			verticalPanel.setSpacing(2);
			sp.setSize("130px", "128px");
			initWidget(sp);
//			verticalPanel.setSize("130px", "128px");
//
//			Image image = new Image("");
//			image.setStyleName((String) null);
//			image.addMouseOverHandler(new MouseOverHandler() {
//				public void onMouseOver(MouseOverEvent event) {
//				}
//			});
//			verticalPanel.add(image);
//			verticalPanel.setCellHeight(image, "60%");
//			image.setSize("128px", "72px");
//
//			InlineLabel VodTitle = new InlineLabel(
//					"Video Title, Video Title, Video Title");
//			VodTitle.addClickHandler(new ClickHandler() {
//				public void onClick(ClickEvent event) {
//					YouTube.getAppGUI().watchVideo(video);
//				}
//			});
//			VodTitle.setDirectionEstimator(true);
//			VodTitle.setStyleName("thumbBox-title");
//			verticalPanel.add(VodTitle);
//			VodTitle.setSize("124px", "27px");
//			verticalPanel.setCellHeight(VodTitle, "20%");
//
//			InlineLabel nlnlblViews = new InlineLabel("author");
//			nlnlblViews.setStyleName("thumbBox-views");
//			verticalPanel.add(nlnlblViews);
		}
	}

  private ytVideoEntry item;

}
